package com.he.hsdyc.security.token;

import com.he.hsdyc.model.User;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * jwt扩展类
 * Created with IntelliJ IDEA.
 * User2: jc
 * Date: 2019-06-11
 * Time: 15:56
 */
public class JwtTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {

        Map<String,Object> info = new HashMap<>();
        info.put("id", ((User) oAuth2Authentication.getPrincipal()).getId());
        info.put("username",((User) oAuth2Authentication.getPrincipal()).getUsername());
        info.put("name",((User) oAuth2Authentication.getPrincipal()).getName());
        info.put("icon",((User) oAuth2Authentication.getPrincipal()).getIcon());
        info.put("tel",((User) oAuth2Authentication.getPrincipal()).getTel());
        info.put("gender",((User) oAuth2Authentication.getPrincipal()).getGender());
        info.put("roleName",((User) oAuth2Authentication.getPrincipal()).getRoleName());
        info.put("auth",((User) oAuth2Authentication.getPrincipal()).getAuth());
        info.put("subjectType",((User) oAuth2Authentication.getPrincipal()).getSubjectType());
        info.put("del",((User) oAuth2Authentication.getPrincipal()).getDel());

        //设置附加信息
        ((DefaultOAuth2AccessToken)oAuth2AccessToken).setAdditionalInformation(info);
        return oAuth2AccessToken;
    }
}
